package com.gw.vip.server.service.tool_service;

import com.gw.vip.common.constants.VipConstants;
import com.gw.vip.common.enums.StatusEnums;
import com.gw.vip.common.util.Snowflake;
import com.gw.vip.server.dto.*;
import com.gw.vip.server.enums.OrderTypeEnums;
import com.gw.vip.server.po.Account;
import com.gw.vip.server.po.Trans;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author GW 光武
 * @description
 * @since 2023-03-12
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccountTransToolService {

    private final Snowflake snowflake;

    public Account initAccount(AccOpenReq req) {
        Account account = new Account();
        account.setId(snowflake.nextStringId());
        account.setCapitalId(req.getCapitalId());
        account.setAccType(req.getAccType());
        account.setUserId(req.getUserId());
        account.setPan(req.getPan());
        account.setAmt(BigDecimal.ZERO);
        account.setLockAmt(BigDecimal.ZERO);
        account.setLastAmt(BigDecimal.ZERO);
        account.setLastLockAmt(BigDecimal.ZERO);
        account.setClearAmt(BigDecimal.ZERO);
        account.setStatus(StatusEnums.SUCCESS.getCode());
        account.setCrtTime(LocalDateTime.now());
        account.setCrtName(req.getCrtName());
        account.setUpdTime(account.getCrtTime());
        account.setUpdName(req.getCrtName());
        return account;
    }

    /**
     * 开户
     * @param req
     * @param account
     * @return
     */
    public Trans initTrans(AccOpenReq req, Account account) {
        Trans trans = new Trans();
        trans.setId(snowflake.nextStringId());
        trans.setClientTransId(req.getClientTransId());
        trans.setOrderId(req.getOrderId());
        trans.setUserId(account.getUserId());
        trans.setPan(account.getPan());
        trans.setAccount(account.getId());
        trans.setCapitalId(account.getCapitalId());
        trans.setAccType(account.getAccType());
        trans.setOrderType(OrderTypeEnums.OPEN.getKey());
        trans.setTxnAmt(account.getAmt());
        trans.setBalAmt(account.getAmt());
        trans.setRefundAmt(BigDecimal.ZERO);
        trans.setStatus(StatusEnums.TRANSACTION.getCode());
        trans.setCompleteTime(req.getTxnDateTime());
        trans.setCrtTime(req.getTxnDateTime());
        trans.setCrtName(req.getCrtName());
        trans.setUpdTime(trans.getCrtTime());
        trans.setUpdName(trans.getCrtName());
        return trans;
    }

    /**
     * 充值
     * @param req
     * @param account
     * @return
     */
    public Trans initTrans(AccChargeReq req, Account account) {
        Trans trans = new Trans();
        trans.setId(snowflake.nextStringId());
        trans.setClientTransId(req.getClientTransId());
        trans.setOrderId(req.getOrderId());
        trans.setUserId(req.getUserId());
        trans.setPan(account.getPan());
        trans.setAccount(account.getId());
        trans.setCapitalId(account.getCapitalId());
        trans.setAccType(account.getAccType());
        trans.setOrderType(OrderTypeEnums.CHARGE.getKey());
        trans.setTxnAmt(req.getTxnAmt());
        trans.setBalAmt(BigDecimal.ZERO);
        trans.setRefundAmt(BigDecimal.ZERO);
        trans.setStatus(StatusEnums.ORIGINAL.getCode());
        trans.setCompleteTime(req.getTxnDateTime());
        trans.setCrtTime(req.getTxnDateTime());
        trans.setCrtName(VipConstants.SYS);
        trans.setUpdTime(trans.getCrtTime());
        trans.setUpdName(trans.getCrtName());
        return trans;
    }

    /**
     * 消费
     * @param req
     * @param account
     * @return
     */
    public Trans initTrans(AccSaleReq req, Account account) {
        Trans trans = new Trans();
        trans.setId(snowflake.nextStringId());
        trans.setClientTransId(req.getClientTransId());
        trans.setOrderId(req.getOrderId());
        trans.setUserId(account.getUserId());
        trans.setPan(account.getPan());
        trans.setAccount(account.getId());
        trans.setCapitalId(account.getCapitalId());
        trans.setAccType(account.getAccType());
        trans.setOrderType(OrderTypeEnums.SALE.getKey());
        trans.setTxnAmt(req.getTxnAmt());
        trans.setBalAmt(BigDecimal.ZERO);
        trans.setRefundAmt(BigDecimal.ZERO);
        trans.setStatus(StatusEnums.ORIGINAL.getCode());
        trans.setCompleteTime(req.getTxnDateTime());
        trans.setCrtTime(req.getTxnDateTime());
        trans.setCrtName(VipConstants.SYS);
        trans.setUpdTime(trans.getCrtTime());
        trans.setUpdName(trans.getCrtName());
        return trans;
    }

    /**
     * 退款。支持部分退款
     * @param req
     * @param account
     * @param srcTrans
     * @return
     */
    public Trans initTrans(AccRefundReq req, Account account, Trans srcTrans) {
        Trans trans = new Trans();
        trans.setId(snowflake.nextStringId());
        trans.setClientTransId(req.getClientTransId());
        trans.setOrderId(req.getOrderId());
        trans.setUserId(srcTrans.getUserId());
        trans.setPan(account.getPan());
        trans.setAccount(account.getId());
        trans.setCapitalId(account.getCapitalId());
        trans.setAccType(account.getAccType());
        trans.setOrderType(OrderTypeEnums.REFUND.getKey());
        trans.setTxnAmt(req.getTxnAmt());
        trans.setBalAmt(BigDecimal.ZERO);
        trans.setSrcId(srcTrans.getId());
        trans.setRefundAmt(BigDecimal.ZERO);
        trans.setStatus(StatusEnums.ORIGINAL.getCode());
        trans.setCompleteTime(req.getTxnDateTime());
        trans.setCrtTime(req.getTxnDateTime());
        trans.setCrtName(VipConstants.SYS);
        trans.setUpdTime(trans.getCrtTime());
        trans.setUpdName(trans.getCrtName());
        return trans;
    }

    public Trans initTrans(AccAuthReq req, Account account) {
        Trans trans = new Trans();
        trans.setId(snowflake.nextStringId());
        trans.setClientTransId(req.getClientTransId());
        trans.setOrderId(req.getOrderId());
        trans.setUserId(req.getUserId());
        trans.setPan(account.getPan());
        trans.setPan(account.getPan());
        trans.setAccount(account.getId());
        trans.setCapitalId(account.getCapitalId());
        trans.setAccType(account.getAccType());
        trans.setOrderType(OrderTypeEnums.AUTH_SALE.getKey());
        trans.setTxnAmt(BigDecimal.ZERO);
        trans.setBalAmt(BigDecimal.ZERO);
        trans.setRefundAmt(BigDecimal.ZERO);
        trans.setAuthAmt(req.getAuthAmt());
        trans.setStatus(StatusEnums.ORIGINAL.getCode());
        trans.setCompleteTime(req.getTxnDateTime());
        trans.setCrtTime(req.getTxnDateTime());
        trans.setCrtName(VipConstants.SYS);
        trans.setUpdTime(trans.getCrtTime());
        trans.setUpdName(trans.getCrtName());
        return trans;
    }
}
